<?php
namespace app\index\controller;
use think\Controller;
use app\index\common;
use think\Db;

use think\Session;
use think\Request;
use app\index\controller\Log\InsertLog as IL;
use app\index\controller\Log\OperationLog as OL;
use app\index\controller\Dropdown;
use think\Paginator;
use think\Config;



class Order extends Controller
{
     /**
     * 2018-08-21
     * 
     * 刘家腾
     * 
     *  基础SQL where
     *
     * @param      string  $data      The data
     * @param      string  $field_data  The field_data
     * @param      string  $page     The page
     * @param      string  $singlepage     The singlepage
     */
    public function list_sql($data='',$field_data='',$page='',$singlepage='')
    {
        
       $product_data = Db::table('orders')->alias('or')
                    ->join('users ui','or.user_id=ui.id','LEFT')
                    ->join('orders_info oi','or.batchcode=oi.batchcode','LEFT')
                    ->join('sales_condition sc','or.status=sc.id','LEFT')
                    ->join('sales_plateform sp','or.sale_system = sp.id','LEFT')
                    ->Distinct(true)
                    ->where($data)
                    ->field($field_data)
                    ->paginate($singlepage,false,['query'=>request()->param()])
                    ->toArray();
       // $product_data['sql'] =Db::table('orders')->getLastSql();
       return $product_data;
    }

    public function orders_info_sql($data='',$field_info='')
    {
       $orders_infos = Db::table('orders_info')->alias('or')
                    ->join('store s','or.store_id=s.user_id','LEFT')
                    ->join('product p','or.product_id=p.id','LEFT')
                    ->join('product_fineness pf','p.product_condition = pf.id','LEFT')
                    ->join('perchase_source ps','p.procurement_status = ps.id','LEFT')
                    ->join('user_info ui','or.user_id=ui.user_id','LEFT')
                    ->join('users u','or.user_id=u.id','LEFT')
                    ->join('staff st','or.staff_id=st.user_id','LEFT')
                    ->join('shipping_mode sm','or.shipping_code=sm.id','LEFT')
                    ->join('sales_condition sc','or.status=sc.id','LEFT')
                    ->where($data)
                    ->where('or.state',0)
                    ->field($field_info) 
                    ->select();     
       $orders_info = collection($orders_infos)->toarray();
       return $orders_info;
    }
                    
    /**
     * 2018-08-21
     * 
     * 刘家腾
     * 
     * 订单列表
     */
    public function list(Request $request)
    {
        $postData = $request->post();
        $user_info=Session::get('user');
        if($user_info['is_business']!=2){
            return ['code' => 30050, 'msg' => "非总平台账号 不能登陆！", 'data' => null];
        }
        $data=array();
        $singlepage=20;
        $page = 1;
        $data['or.state']  = array('eq',0);//启用
        $data['or.type']  = array('eq',3);//小程序   
        if( !empty($postData['business_id']) ){
            $data['or.business_id']  = array('eq',$postData['business_id'] );//商户
        }                               
        if( !empty($postData['singlepage']) ){
            $singlepage  = $postData['singlepage'];
        }
        if( !empty($postData['batchcode']) ){
            $data['or.batchcode']  = array('eq',$postData['batchcode']);
        }
        if( !empty($postData['phone']) ){
            $data['ui.phone']  = array('eq',$postData['phone']);
        }
        
        if( !empty($postData['status']) ){
            $data['or.status']  = array('eq',$postData['status']);
        }
        if( !empty($postData['payment_status']) ){
            $data['oi.payment_status']  = array('eq',$postData['payment_status']);
            $data_info['or.payment_status']  = array('eq',$postData['payment_status']);
        }
        if( !empty($postData['page']) ){
            $page = $postData['page'];
        }
        if( !empty($postData['shipping_sn']) ){
            $data['oi.shipping_sn']  = array('eq',$postData['shipping_sn']);
        }
        
        $field_data = 'or.id,or.batchcode,or.pay_batchcode,or.transaction_id,or.user_id,or.total_price,or.total_rebate,or.total_fare,or.coupon_id,or.coupon_price,or.quantity,or.payment_status,or.status,or.last_time,or.createtime,or.pay_time,or.rights_time,or.refund_time,or.remark,or.consignee,or.delivery_id,or.is_urgent,or.urgent_remark,or.cover,or.order_source,or.sale_system,sc.value status_value,sp.value system_value,ui.phone,ui.name';

        $field_info ='or.id,or.peer_price,or.business_id,or.store_id,or.user_id,or.batchcode,or.pay_batchode,or.info_batchcode,or.product_id,or.product_name,or.coupon_id,or.express_id,or.price,or.cost_price,or.fare,or.status,or.pequired_points,or.quantity,or.pay_reatetime,or.rights_time,or.refund_time,or.last_time,or.payment_status,or.createtime,or.remark,or.is_prepay,or.deposit,or.shipping_sn,or.sale_date,or.nursing_fee,or.other_fee,or.staff_id,or.nursing_cost,or.nursing_income,or.nursing_condition,or.nursing_remark,or.customer_name,or.customer_id,or.is_urgent,or.urgent_remark,or.cover,or.order_source,or.is_confirm,or.finance_confirm_time,or.parts,or.is_settlement,or.is_nursing,or.procurement_status,or.settlement_time,p.product_name,p.full_name,p.product_sn,p.product_condition,pf.value product_condition_value,p.procurement_status, ps.value procurement_value,p.cost_price,p.pic_url,st.name staff_name,p.additional,p.systematics_id,st.name,p.product_source,p.sale_share,p.receive_share,p.thumb_url,p.original_price,s.store_name,sm.shipping_name,sc.value status_value';

        $datas=$this->list_sql($data,$field_data,$page,$singlepage);
        if ( empty($datas['data']) ) {
            return ['code' => 666, 'msg' => "没有查询到类似数据！", 'data' => null];
        }
        foreach ($datas['data'] as $key => $value) {
            $data_info['or.batchcode']  = array('eq',$value['batchcode']);
            $datas['data'][$key]['product_data']=$this->orders_info_sql($data_info,$field_info);
        }
        return ['code' => 666, 'msg' => "查询成功！", 'data' => $datas];
    }

    /*
    * 2018-08-22
    * 刘家腾
    */
    public function withdraw_culist_sql($data='',$field_info='',$page='',$singlepage='',$createtime='',$between_c='',$interval_c='',$updatetime='',$between_u='',$interval_u='')
    {
       $orders_infos = Db::table('card_balance_log')->alias('c')
                    ->join('users u','c.user_id=u.id','LEFT')
                    ->where($data)
                    ->whereTime($createtime,$between_c,$interval_c)
                    ->whereTime($updatetime,$between_u,$interval_u)
                    ->field($field_info) 
                    ->paginate($singlepage,false,['query'=>request()->param()])
                    ->toArray();
       return $orders_infos;
    }
    public function withdraw_list_sql($data='',$field_info='',$page='',$singlepage='',$createtime='',$between='',$interval='')
    {
       $orders_infos = Db::table('card_balance_log')->alias('c')
                    ->join('users u','c.user_id=u.id','LEFT')
                    ->where($data)
                    ->whereTime($createtime,$between,$interval)
                    ->field($field_info) 
                    ->paginate($singlepage,false,['query'=>request()->param()])
                    ->toArray();
       return $orders_infos;
    }
    public function withdraw_sql($data='',$field_info='',$page='',$singlepage='')
    {
       $orders_infos = Db::table('card_balance_log')->alias('c')
                    ->join('users u','c.user_id=u.id','LEFT')
                    ->where($data)
                    ->field($field_info) 
                    ->paginate($singlepage,false,['query'=>request()->param()])
                    ->toArray();
       return $orders_infos;
    }
    /**
     * 2018-08-15
     * 
     * 刘家腾
     * 
     * 提现
     */
    public function withdraw_list(Request $request)
    {
        $postData = $request->post();
        $user_info=Session::get('user');
        if($user_info['is_business']!=2){
            return ['code' => 30050, 'msg' => "非总平台账号 不能登陆！", 'data' => null];
        }
        $data=array();
        $singlepage=20;
        $page = 1;
        $data['c.state']  = array('eq',0);//启用
        if( !empty($postData['business_id']) ){
            $data['c.user_id']  = array('eq',$postData['business_id']);//商户
        }
        // 0充值 1提现 2交易进账 3交易出账
        if( !empty($postData['type']) ){
            $data['c.type']  = array('eq',$postData['type']);
        }                               
        if( !empty($postData['singlepage']) ){
            $singlepage  = $postData['singlepage'];
        }
        if( !empty($postData['page']) ){
            $page = $postData['page'];
        }
        $field_info='c.*,u.name,u.phone';
        $create_state=0;
        $update_state=0;
        $createtime='c.createtime';
        if( !empty($postData['start_createtime']) ){
            if( !empty($postData['end_createtime']) ){
                
                $between_c='between';
                $interval_c=array($postData['start_createtime'],$postData['end_createtime']);
            }else{
                
                $between_c='>';
                $interval_c=$postData['start_createtime'];
            }
            $create_state=1;
        }elseif( !empty($postData['end_createtime']) ){
            $create_state=1;
            $between_c='<';
            $interval_c=$postData['start_createtime'];
        }
        $updatetime='c.updatetime';
        if( !empty($postData['start_updatetime']) ){
            if( !empty($postData['end_updatetime']) ){
                
                $between_u='between';
                $interval_u=array($postData['start_updatetime'],$postData['end_updatetime']);
            }else{
                
                $between_u='>';
                $interval_u=$postData['start_updatetime'];
            }
            $update_state=1;
        }elseif( !empty($postData['end_updatetime']) ){
            $update_state=1;
            $between_u='<';
            $interval_u=$postData['start_updatetime'];
        }
        
        if($create_state==1&&$update_state==1){
            $datas=self::withdraw_culist_sql($data,$field_info,$page,$singlepage,$createtime,$between_c,$interval_c,$updatetime,$between_u,$interval_u);
        }elseif($create_state==1){
            $datas=self::withdraw_list_sql($data,$field_info,$page,$singlepage,$createtime,$between_c,$interval_c);
        }elseif($update_state==1){
            $datas=self::withdraw_list_sql($data,$field_info,$page,$singlepage,$updatetime,$between_u,$interval_u);
        }else{
            $datas=self::withdraw_sql($data,$field_info,$page,$singlepage);
        }
        $datas['sql']=Db::table('card_balance_log')->getLastSql();
        if ( empty($datas) ) {
            return ['code' => 666, 'msg' => "操作失败！", 'data' => null];
        }
        return ['code' => 666, 'msg' => "成功！", 'data' => $datas];
        
        
    }
    
  


}
